﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model.Strategies
{
    /// <summary>
    /// 密码安全保护策略
    /// </summary>
    public class CryptoSecurityStrategy : ICryptoSecurityStrategy
    {
        /// <summary>
        /// 错误密码
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public LockResult IncorrectPassword(JzSportDbContext dbContext, Account account)
        {
            AccountRepository acRep = new AccountRepository(dbContext);
            IllegalPasswordRecordRepository rep = new IllegalPasswordRecordRepository(dbContext);

            LockResult result = new LockResult();
            if (account.IsLocked)
            {
                // 密码错误,账号并且已被锁定，密码保护策略执行操作
                // 更新账号锁定时间
                account.IsLocked = true;
                account.UnlockTime = DateTime.Now.AddMinutes(30);
                acRep.Update(account.Id, account);

                result.IsLocked = true;
                result.RetryCount = 0;
                result.LockoutDuration = 30;
            }
            else
            {
                // 密码错误, 账号未被锁定，密码保护策略执行操作
                List<IllegalPasswordRecord> lst = rep.Get(account.Id);
                if (lst.Count >= 4)
                {
                    // 创建一个错误记录
                    IllegalPasswordRecord record = new IllegalPasswordRecord();
                    record.Id = Guid.NewGuid();
                    record.IsValid = true;
                    record.OccurringTime = DateTime.Now;
                    record.UserId = account.Id;
                    rep.Create(record);

                    // 锁定账号
                    account.IsLocked = true;
                    account.UnlockTime = DateTime.Now.AddMinutes(30);
                    acRep.Update(account.Id, account);

                    result.IsLocked = true;
                    result.RetryCount = 0;
                    result.LockoutDuration = 30;
                }
                else
                {
                    // 创建一个错误记录
                    IllegalPasswordRecord record = new IllegalPasswordRecord();
                    record.Id = Guid.NewGuid();
                    record.IsValid = true;
                    record.OccurringTime = DateTime.Now;
                    record.UserId = account.Id;
                    rep.Create(record);

                    result.IsLocked = false;
                    result.RetryCount = 5 - lst.Count - 1;
                }
            }
            return result;
        }

        /// <summary>
        /// 输入正确密码
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public LockResult CorrectPassword(JzSportDbContext dbContext, Account account)
        {
            AccountRepository acRep = new AccountRepository(dbContext);
            IllegalPasswordRecordRepository rep = new IllegalPasswordRecordRepository(dbContext);

            LockResult result = new LockResult();

            // 去除非法密码记录
            List<IllegalPasswordRecord> lst = rep.Get(account.Id);
            for (int index = 0; index < lst.Count; index++)
            {
                lst[index].IsValid = false;
                rep.Update(lst[index]);
            }

            // 解锁账号
            account.IsLocked = false;
            account.UnlockTime = null;
            acRep.Update(account.Id, account);

            result.IsLocked = false;
            result.RetryCount = 0;
            result.LockoutDuration = 0;

            return result;
        }
    }
}
